<?php include_once ('function/check_login_function.php'); onlyValid(); onlyDoctor();?>
<!DOCTYPE html>
<html>
<head>
	<title>View Request</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" type="text/css" href="">
</head>
<body>
	<div id="wrap">
		<header>
		</header>
		<div id="main">
			<div id="menu">
			</div>
			<div id="content">
				<?php 
					if (isset($_GET['msg'])) {
						if (intval($_GET['msg']) === 1) {
							echo '<p>You have successfully removed a request.</p>';
						} else if (intval($_GET['msg']) === 2) {
							echo '<p>You have successfully accepted a request.</p>';
						}
					}
					
					if (isset($_GET['patient'])) {
						$patient_id = $_GET['patient'];
						if ($user->isPatient($patient_id)) {
							$patient = Patient::findPatient($patient_id);
							echo '<h>' . User::findName($patient_id) . '</h>';
							echo '<ul>';
							echo '<li>' . $patient['user_email'] . '</li>';
							echo '<li>' . $patient['user_gender'] . '</li>';
							echo '<li>' . $patient['user_address'] . '</li>';
							echo '<li>' . $patient['user_contact'] . '</li>';
							echo '<li>' . $patient['user_dob'] . '</li>';
							echo '<li>' . $patient['patient_bloodtype'] . '</li>';
							echo '<li>' . $patient['patient_allergy'] . '</li>';				
							echo '</ul>';
							$datalog_list = DataLog::getDataLogList($patient_id);
							
							if ($datalog_list !== null) { 
								echo '<table>';
								echo '<tr>';
								echo '<th>Type</th>';
								echo '<th>Value</th>';
								echo '<th>Notes</th>';
								echo '<th>Date</th>';
								echo '<th></th>';
								echo '</tr>';
								
								foreach ($datalog_list as $datalog) {
									echo '<tr>';
									echo '<td>' . DataLog::findTypeName($datalog['datalog_type_id']) . '</td>';
									echo '<td>' . $datalog['datalog_value'] . '</td>';
									echo '<td>' . $datalog['datalog_note'] . '</td>';
									echo '<td>' . $datalog['created_at'] . '</td>';
									echo '</tr>';
								}
							}						
							echo '</table> <br />';
							echo '<a href="patients.php">Back</a>';
							
						} else {
							echo 'No such patient. <br />';
							echo 'You are not allowed'; //To be removed
						}
					} else {
				?>
					<p>List of patients</p>
					<ol>
					<?php
						$patient_list = $user->getPatientList();
						if ($patient_list !== null) {
							foreach ($patient_list as $patient) {
								$patient_id = $patient['patient_id'];
								echo '<form action="function/handle_request_function.php" method="POST">';
								echo '<li>';
								echo '<a href="patients.php?patient=' . $patient_id . '">' . User::findName($patient_id) . '</a>';
								if (intval($patient['request_pending']) === 0) {
									echo ' (Accepted) ';
									echo '<input type="hidden" id="patient" name="patient" value="' . $patient_id . '"/>';
									echo '<input type="submit" name="remove" value="Remove Patient" /> ';
									echo '</li>';
									echo' </form>';
								} else {
									echo ' (Pending) ';
									echo '<input type="hidden" id="patient" name="patient" value="' . $patient_id . '"/>';
									echo '<input type="submit" name="accept" value="Accept Patient" /> ';
									echo '<input type="submit" name="remove" value="Remove Patient" /> ';
									echo '</li>';
									echo' </form>';
								}
								$doctor_id = $patient['doctor_id'];
							}
						} else {
							echo 'You have sent no requests at this moment';
						}
					?>
					</ol>
				<?php } ?>
			</div>
		</div>
		<footer>
			<div id="footer">
				
			</div>
		</footer>
	</div>
</body>